In [ ]:
import pandas as pd
import plotly.express as px

#html export
import plotly.io as pio
pio.renderers.default = 'notebook'
In [ ]:
df = pd.read_csv('2019-census-report.csv', decimal=',')
df
Out[ ]:
County Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
0 Baringo 666763 336322 330428 142518 5 10976 61 555561 111202 13
1 Bomet 875689 434287 441379 187641 5 2531 346 724186 151503 23
2 Bungoma 1670570 812146 858389 358796 5 3024 552 1630934 39636 35
3 Busia 893681 426252 467401 198152 5 1696 527 488075 405606 28
4 Elgeyo-Marakwet 454480 227317 227151 99861 5 3032 150 369998 84482 12
5 Embu 608599 304208 304367 182743 3 2821 216 516212 92387 24
6 Garissa 841353 458975 382344 141394 6 44736 19 623060 218293 34
7 Homa Bay 1131950 539560 592367 262036 4 3153 359 963794 168156 23
8 Isiolo 268002 139510 128483 58072 5 25350 11 143294 124708 9
9 Kajiado 1117840 557098 560704 316179 4 21871 51 687312 430528 38
10 Kakamega 1867579 897133 970406 433207 4 3020 618 1660651 206928 40
11 Kericho 901777 450751 451008 206036 4 2436 370 758339 143438 28
12 Kiambu 2417735 1187146 1230454 795241 3 2539 952 1623282 794453 135
13 Kilifi 1453787 704089 749673 298472 5 12540 116 1109735 344052 25
14 Kirinyaga 610411 302011 308369 204188 3 1478 413 528054 82357 31
15 Kisii 1266860 605784 661038 308054 4 1323 958 1152282 114578 38
16 Kisumu 1155574 560942 594609 300745 4 2085 554 968909 186665 23
17 Kitui 1136187 549003 587151 262942 4 30430 37 1012709 123478 33
18 Kwale 866820 425121 441681 173176 5 8267 105 649931 216889 18
19 Laikipia 518580 259440 259102 149271 3 9532 54 399227 119353 18
20 Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4
21 Machakos 1421932 710707 711191 402466 4 6043 235 1098584 323348 34
22 Makueni 987653 489691 497942 244669 4 8170 121 884527 103126 20
23 Mandera 867457 434976 432444 125763 7 25940 33 1025756 -158299 37
24 Marsabit 459785 243548 216219 77495 6 70944 6 291166 168619 18
25 Meru 1545714 767698 777975 426360 4 7006 221 1356301 189413 41
26 Migori 1116436 536187 580214 240168 5 2614 427 917170 199266 35
27 Mombasa 1208333 610257 598046 378422 3 220 5495 939370 268963 30
28 Murang'a 1056640 523940 532669 318105 3 2524 419 942581 114059 31
29 Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245
30 Nakuru 2162202 1077272 1084835 616046 4 7462 290 1603325 558877 95
31 Nandi 885711 441259 444430 199426 4 2856 310 752965 132746 22
32 Narok 1157873 579042 578805 241125 5 17950 65 850920 306953 26
33 Nyamira 605576 290907 314656 150669 4 897 675 598252 7324 13
34 Nyandarua 638289 315022 323247 179686 4 3286 194 596268 42021 20
35 Nyeri 759164 374288 384845 248050 3 3325 228 693558 65606 31
36 Samburu 310327 156774 153546 65910 5 21065 15 223947 86380 7
37 Siaya 993183 471669 521496 250698 4 2530 393 842304 150879 18
38 Taita Taveta 340671 173337 167327 96429 4 17152 20 284657 56014 7
39 Tana River 315943 158550 157391 68242 5 37951 8 240075 75868 2
40 Tharaka-Nithi 393177 193764 199406 109860 4 2564 153 365330 27847 7
41 Trans Nzoia 990341 489107 501206 223808 4 2495 397 818757 171584 28
42 Turkana 926976 478087 448868 164519 6 68233 14 855399 71577 21
43 Uasin Gishu 1163186 580269 582889 304943 4 3392 343 894179 269007 28
44 Vihiga 590013 283678 306323 143365 4 564 1047 554622 35391 12
45 Wajir 781263 415374 365840 127932 6 56773 14 661941 119322 49
46 West Pokot 621241 307013 314213 116182 5 9123 68 512690 108551 15
In [ ]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 47 entries, 0 to 46
Data columns (total 11 columns):
 #   Column                    Non-Null Count  Dtype 
---  ------                    --------------  ----- 
 0   County                    47 non-null     object
 1   Total_Population19        47 non-null     int64 
 2   Male population 2019      47 non-null     int64 
 3   Female population 2019    47 non-null     int64 
 4   Households                47 non-null     int64 
 5   Av_HH_Size                47 non-null     int64 
 6   LandArea                  47 non-null     int64 
 7   Population Density        47 non-null     int64 
 8   Population in 2009        47 non-null     int64 
 9   Pop_change                47 non-null     int64 
 10  Intersex population 2019  47 non-null     int64 
dtypes: int64(10), object(1)
memory usage: 4.2+ KB
In [ ]:
# Setting County as the index for the table
df.index = df['County']
df = df.drop(columns='County')
df
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Baringo 666763 336322 330428 142518 5 10976 61 555561 111202 13
Bomet 875689 434287 441379 187641 5 2531 346 724186 151503 23
Bungoma 1670570 812146 858389 358796 5 3024 552 1630934 39636 35
Busia 893681 426252 467401 198152 5 1696 527 488075 405606 28
Elgeyo-Marakwet 454480 227317 227151 99861 5 3032 150 369998 84482 12
Embu 608599 304208 304367 182743 3 2821 216 516212 92387 24
Garissa 841353 458975 382344 141394 6 44736 19 623060 218293 34
Homa Bay 1131950 539560 592367 262036 4 3153 359 963794 168156 23
Isiolo 268002 139510 128483 58072 5 25350 11 143294 124708 9
Kajiado 1117840 557098 560704 316179 4 21871 51 687312 430528 38
Kakamega 1867579 897133 970406 433207 4 3020 618 1660651 206928 40
Kericho 901777 450751 451008 206036 4 2436 370 758339 143438 28
Kiambu 2417735 1187146 1230454 795241 3 2539 952 1623282 794453 135
Kilifi 1453787 704089 749673 298472 5 12540 116 1109735 344052 25
Kirinyaga 610411 302011 308369 204188 3 1478 413 528054 82357 31
Kisii 1266860 605784 661038 308054 4 1323 958 1152282 114578 38
Kisumu 1155574 560942 594609 300745 4 2085 554 968909 186665 23
Kitui 1136187 549003 587151 262942 4 30430 37 1012709 123478 33
Kwale 866820 425121 441681 173176 5 8267 105 649931 216889 18
Laikipia 518580 259440 259102 149271 3 9532 54 399227 119353 18
Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4
Machakos 1421932 710707 711191 402466 4 6043 235 1098584 323348 34
Makueni 987653 489691 497942 244669 4 8170 121 884527 103126 20
Mandera 867457 434976 432444 125763 7 25940 33 1025756 -158299 37
Marsabit 459785 243548 216219 77495 6 70944 6 291166 168619 18
Meru 1545714 767698 777975 426360 4 7006 221 1356301 189413 41
Migori 1116436 536187 580214 240168 5 2614 427 917170 199266 35
Mombasa 1208333 610257 598046 378422 3 220 5495 939370 268963 30
Murang'a 1056640 523940 532669 318105 3 2524 419 942581 114059 31
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245
Nakuru 2162202 1077272 1084835 616046 4 7462 290 1603325 558877 95
Nandi 885711 441259 444430 199426 4 2856 310 752965 132746 22
Narok 1157873 579042 578805 241125 5 17950 65 850920 306953 26
Nyamira 605576 290907 314656 150669 4 897 675 598252 7324 13
Nyandarua 638289 315022 323247 179686 4 3286 194 596268 42021 20
Nyeri 759164 374288 384845 248050 3 3325 228 693558 65606 31
Samburu 310327 156774 153546 65910 5 21065 15 223947 86380 7
Siaya 993183 471669 521496 250698 4 2530 393 842304 150879 18
Taita Taveta 340671 173337 167327 96429 4 17152 20 284657 56014 7
Tana River 315943 158550 157391 68242 5 37951 8 240075 75868 2
Tharaka-Nithi 393177 193764 199406 109860 4 2564 153 365330 27847 7
Trans Nzoia 990341 489107 501206 223808 4 2495 397 818757 171584 28
Turkana 926976 478087 448868 164519 6 68233 14 855399 71577 21
Uasin Gishu 1163186 580269 582889 304943 4 3392 343 894179 269007 28
Vihiga 590013 283678 306323 143365 4 564 1047 554622 35391 12
Wajir 781263 415374 365840 127932 6 56773 14 661941 119322 49
West Pokot 621241 307013 314213 116182 5 9123 68 512690 108551 15

Comparative Analysis¶

Total Male population and Total Female population comparison

In [ ]:
# creating a series for required values
mvf = pd.Series([df['Male population 2019'].sum(),df['Female population 2019'].sum()], index=['Total Male population','Total Female population'])
mvf
Out[ ]:
Total Male population      23548066
Total Female population    24014716
dtype: int64
In [ ]:
# using the series to plot a pie chart
fig = px.pie(names=mvf.index, values=mvf)

fig.update_layout(title='Male vs Female Population')

fig.show()

Determining Population growth

In [ ]:
# creating a series with required values for graph
pop_change = pd.Series([df['Population in 2009'].sum(),df['Total_Population19'].sum()], index=['Population in 2009', 'Population in 2019'])
pop_change
Out[ ]:
Population in 2009    38610097
Population in 2019    47564316
dtype: int64
In [ ]:
# Creating a bar graph using Plotly Express
fig = px.bar(x=pop_change.index, y=pop_change, title='Population Change (2009-2019)')
fig.update_layout(yaxis_title='Population')

# Show the plot
fig.show()
In [ ]:
# population growth %
((df['Total_Population19'].sum() - df['Population in 2009'].sum())/ df['Population in 2009'].sum())*100
Out[ ]:
23.191392137657672

Population Growth is at 23% over 10 years

Descriptive Analysis¶

Largest Population¶

In [ ]:
# County with the largest population
df[df['Total_Population19'] == df['Total_Population19'].max()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245
In [ ]:
# County with the largest male population
df[df['Male population 2019'] == df['Male population 2019'].max()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245
In [ ]:
# County with the largest female population
df[df['Female population 2019'] == df['Female population 2019'].max()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245

Nairobi has both the largest male and female population

Smallest Population¶

In [ ]:
# County with the smallest population
df[df['Total_Population19'] == df['Total_Population19'].min()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4
In [ ]:
# County with the smallest male population
df[df['Male population 2019'] == df['Male population 2019'].min()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4
In [ ]:
# County with the largest female population
df[df['Female population 2019'] == df['Female population 2019'].min()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4

Lamu has both the smallest male and female population

Average household size¶

In [ ]:
df['Av_HH_Size'].mean()
Out[ ]:
4.340425531914893

Average number of people in each household is 4 people

Population change¶

In [ ]:
# County with highest population change
df[df['Pop_change'] == df['Pop_change'].max()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245
In [ ]:
# creating a series with required values for graph
nai_pop_change = df.loc['Nairobi', ['Total_Population19', 'Population in 2009', 'Pop_change']]
nai_pop_change = pd.Series(nai_pop_change)
print(nai_pop_change)
print()
print((nai_pop_change[2]/nai_pop_change[0])*100)


'''
nai_pop_change = pd.Series([df['Total_Population19'].max(),df['Population in 2009'].max()], index=['Total Population', 'Population in 2019'])
print(nai_pop_change)
((nai_pop_change[0]-nai_pop_change[1])/nai_pop_change[0])*100
'''
Total_Population19    4397073
Population in 2009    3138369
Pop_change            1258704
Name: Nairobi, dtype: int64

28.625951854790678
Out[ ]:
"\nnai_pop_change = pd.Series([df['Total_Population19'].max(),df['Population in 2009'].max()], index=['Total Population', 'Population in 2019'])\nprint(nai_pop_change)\n((nai_pop_change[0]-nai_pop_change[1])/nai_pop_change[0])*100\n"

Nairobi has the largest population growth at +1,258,704 with the percentage growth at 29%

In [ ]:
# County with least population change
df[df['Pop_change'] == df['Pop_change'].min()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019
County
Mandera 867457 434976 432444 125763 7 25940 33 1025756 -158299 37
In [ ]:
man_pop_change = df.loc['Mandera', ['Total_Population19', 'Population in 2009', 'Pop_change']]
man_pop_change = pd.Series(man_pop_change)
print(man_pop_change)
(man_pop_change[2]/man_pop_change[0])*100
Total_Population19     867457
Population in 2009    1025756
Pop_change            -158299
Name: Mandera, dtype: int64
Out[ ]:
-18.248627885877916

Mandera has the smallest population growth at -158,299 with the percentage growth at -18%

Percentage Growth¶

In [ ]:
df['%_Growth'] = (df['Pop_change']/df['Total_Population19'])*100
df
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019 %_Growth
County
Baringo 666763 336322 330428 142518 5 10976 61 555561 111202 13 16.677890
Bomet 875689 434287 441379 187641 5 2531 346 724186 151503 23 17.301005
Bungoma 1670570 812146 858389 358796 5 3024 552 1630934 39636 35 2.372603
Busia 893681 426252 467401 198152 5 1696 527 488075 405606 28 45.385993
Elgeyo-Marakwet 454480 227317 227151 99861 5 3032 150 369998 84482 12 18.588717
Embu 608599 304208 304367 182743 3 2821 216 516212 92387 24 15.180275
Garissa 841353 458975 382344 141394 6 44736 19 623060 218293 34 25.945471
Homa Bay 1131950 539560 592367 262036 4 3153 359 963794 168156 23 14.855426
Isiolo 268002 139510 128483 58072 5 25350 11 143294 124708 9 46.532489
Kajiado 1117840 557098 560704 316179 4 21871 51 687312 430528 38 38.514278
Kakamega 1867579 897133 970406 433207 4 3020 618 1660651 206928 40 11.080013
Kericho 901777 450751 451008 206036 4 2436 370 758339 143438 28 15.906150
Kiambu 2417735 1187146 1230454 795241 3 2539 952 1623282 794453 135 32.859391
Kilifi 1453787 704089 749673 298472 5 12540 116 1109735 344052 25 23.665915
Kirinyaga 610411 302011 308369 204188 3 1478 413 528054 82357 31 13.492057
Kisii 1266860 605784 661038 308054 4 1323 958 1152282 114578 38 9.044251
Kisumu 1155574 560942 594609 300745 4 2085 554 968909 186665 23 16.153444
Kitui 1136187 549003 587151 262942 4 30430 37 1012709 123478 33 10.867753
Kwale 866820 425121 441681 173176 5 8267 105 649931 216889 18 25.021227
Laikipia 518580 259440 259102 149271 3 9532 54 399227 119353 18 23.015350
Lamu 143920 76103 67813 37963 4 6253 23 101539 42381 4 29.447610
Machakos 1421932 710707 711191 402466 4 6043 235 1098584 323348 34 22.740047
Makueni 987653 489691 497942 244669 4 8170 121 884527 103126 20 10.441521
Mandera 867457 434976 432444 125763 7 25940 33 1025756 -158299 37 -18.248628
Marsabit 459785 243548 216219 77495 6 70944 6 291166 168619 18 36.673445
Meru 1545714 767698 777975 426360 4 7006 221 1356301 189413 41 12.254078
Migori 1116436 536187 580214 240168 5 2614 427 917170 199266 35 17.848403
Mombasa 1208333 610257 598046 378422 3 220 5495 939370 268963 30 22.259013
Murang'a 1056640 523940 532669 318105 3 2524 419 942581 114059 31 10.794500
Nairobi 4397073 2192452 2204376 1506888 3 704 6247 3138369 1258704 245 28.625952
Nakuru 2162202 1077272 1084835 616046 4 7462 290 1603325 558877 95 25.847585
Nandi 885711 441259 444430 199426 4 2856 310 752965 132746 22 14.987507
Narok 1157873 579042 578805 241125 5 17950 65 850920 306953 26 26.510075
Nyamira 605576 290907 314656 150669 4 897 675 598252 7324 13 1.209427
Nyandarua 638289 315022 323247 179686 4 3286 194 596268 42021 20 6.583382
Nyeri 759164 374288 384845 248050 3 3325 228 693558 65606 31 8.641874
Samburu 310327 156774 153546 65910 5 21065 15 223947 86380 7 27.835155
Siaya 993183 471669 521496 250698 4 2530 393 842304 150879 18 15.191460
Taita Taveta 340671 173337 167327 96429 4 17152 20 284657 56014 7 16.442257
Tana River 315943 158550 157391 68242 5 37951 8 240075 75868 2 24.013192
Tharaka-Nithi 393177 193764 199406 109860 4 2564 153 365330 27847 7 7.082561
Trans Nzoia 990341 489107 501206 223808 4 2495 397 818757 171584 28 17.325749
Turkana 926976 478087 448868 164519 6 68233 14 855399 71577 21 7.721559
Uasin Gishu 1163186 580269 582889 304943 4 3392 343 894179 269007 28 23.126740
Vihiga 590013 283678 306323 143365 4 564 1047 554622 35391 12 5.998342
Wajir 781263 415374 365840 127932 6 56773 14 661941 119322 49 15.272962
West Pokot 621241 307013 314213 116182 5 9123 68 512690 108551 15 17.473251
In [ ]:
# County with highest percentage population change
df[df['%_Growth'] == df['%_Growth'].max()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019 %_Growth
County
Isiolo 268002 139510 128483 58072 5 25350 11 143294 124708 9 46.532489

Isiolo has the largest percentage population growth at 47%

In [ ]:
# County with lowest percentage population change
df[df['%_Growth'] == df['%_Growth'].min()]
Out[ ]:
Total_Population19 Male population 2019 Female population 2019 Households Av_HH_Size LandArea Population Density Population in 2009 Pop_change Intersex population 2019 %_Growth
County
Mandera 867457 434976 432444 125763 7 25940 33 1025756 -158299 37 -18.248628

Mandera has the lowest percentage population growth at -18%